package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Employee;
import com.darkplanet.management.domain.EmployeeRole;
import com.darkplanet.management.domain.Role;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

public class EmployeeRoleDaoImp implements IEmployeeRoleDao {

    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public List<EmployeeRole> getEmployeeRolesById(Integer employee_id) {
        String sql = "select * from employeerole where employee_id = ?";
        List<EmployeeRole> roles = null;
        try {
            roles = queryRunner.query(sql, new BeanListHandler<EmployeeRole>(EmployeeRole.class), employee_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return roles;
    }
    


    @Override
    public void addEmployeeRole(Integer employee_id,Integer role_id) {
        String sql = "insert into employeerole (employee_id,role_id) values(?,?)";
        try {
            queryRunner.update(sql,employee_id,role_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteEmployeeRoleById(Integer id) {
        String sql = "delete from employeerole where employee_id = ?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateEmployeeRoleById(Integer role_id,Integer employee_id) {
        String sql = "update employeerole set role_id=? where employee_id = ?";
        try {
            queryRunner.update(sql,role_id,employee_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
